package org.example.actualcombat.demos.web.modules.pms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.actualcombat.demos.web.common.api.CommonPage;
import org.example.actualcombat.demos.web.common.api.CommonResult;
import org.example.actualcombat.demos.web.modules.pms.model.PmsProductAttributeCategory;
import org.example.actualcombat.demos.web.modules.pms.model.PmsProductCategoryModel;
import org.example.actualcombat.demos.web.modules.pms.model.dto.ProductAttributeCategoryDTO;
import org.example.actualcombat.demos.web.modules.pms.service.PmsProductAttributeCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
/*
* 商品类型-主列表 - 增删改查
* */
@RestController
@RequestMapping("/productAttribute/category")
public class PmsProductAttributeCategoryController {
    @Autowired
    PmsProductAttributeCategoryService pmsProductAttributeCategoryService;

    /*
    *
    *   url:'/productAttribute/category/list',
        method:'get',
        params:params
    * */
    @RequestMapping(value = "/list",method = RequestMethod.GET) // 直接获取路径里的值
    /*
    * 返回一个list,一个total . 都是在一个data对象里
    * */
    public CommonResult<CommonPage<PmsProductAttributeCategory>> getList(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "3") Integer pageSize
            ){

        Page page  =  pmsProductAttributeCategoryService.list(pageNum,pageSize);
        return CommonResult.success(CommonPage.restPage(page)); // 传入分页数据, 然后格式化为Page对象,然后再格式化为正确数据的格式
    }
        /*
        * 筛选属性 - 下拉级联
            url:'/productAttribute/category/list/withAttr',
            method:'get'
        * */
    @RequestMapping(value = "/list/withAttr",method = RequestMethod.GET)
    public CommonResult getListWithAttr(){

       List<ProductAttributeCategoryDTO> pacModel =   pmsProductAttributeCategoryService.getListWithAttr();
        return CommonResult.success(pacModel);
    }


    /*
    *
    *    url:'/productAttribute/category/create',
        method:'post',
        data:data
    * */
    @RequestMapping(value = "/create",method = RequestMethod.POST)
    public CommonResult createCategory(@RequestParam(value = "name") String name){
        boolean res = pmsProductAttributeCategoryService.add(name);
        if(res){
            return  CommonResult.success(res);
        }else{
            return  CommonResult.failed();
        }
    }


    /*
    url:'/productCategory/delete/'+id,
    method:'post'
*/
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public CommonResult delete(@PathVariable Long id){
        boolean res = pmsProductAttributeCategoryService.deleteVal(id);
        if(res){
            return  CommonResult.success(res);
        }else{
            return  CommonResult.failed();
        }
    }

    /*
    *
    *
    *   url:'/productCategory/update/navStatus',
        method:'post',
        data
    * */
//    @RequestMapping(value = "/update/navStatus",method = RequestMethod.POST)
//    public CommonResult UpdateNavStatus(
//            @RequestParam(value = "ids") List<Long> ids,
//            @RequestParam(value = "navStatus") Integer navStatus
//        ){
//        boolean res = pmsProductAttributeCategoryService.UpdateNavStatus(ids, navStatus);
//        if(res)  {
//            return CommonResult.success(res);
//        }else{
//            return  CommonResult.failed();
//        }
////        return CommonResult.success(data)
//    }

    /*
    *
    * url:'/productCategory/update/showStatus',
        method:'post',
        data
    *
    * */
//    @RequestMapping(value = "/update/showStatus",method = RequestMethod.POST)
//    public CommonResult showStatus(@RequestParam(value = "showStatus") Integer status, @RequestParam() List<Long> ids){ // 第一个参数是接受的,第二个参数是在当前函数里的值
//       boolean res =  pmsProductAttributeCategoryService.updateStatus(status,ids);
//       if(res){
//           return  CommonResult.success(res);
//       }else{
//           return  CommonResult.failed();
//       }
//    }


    /*
    分类添加
    url:'/productCategory/create',
    method:'post',
    data:data
    */





//    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
//    public CommonResult getById(@PathVariable Integer id){
//        PmsProductAttributeCategory id1 = pmsProductAttributeCategoryService.getById(id);
//
//        return  CommonResult.success(id1);
//    }


    @RequestMapping(value = "/update/{id}",method = RequestMethod.POST) // 在impl里最终还是使用了对象, 和id没关系.只是用来占位了
    public CommonResult updateCategory(@PathVariable int id,@RequestParam(value = "name")  String name){
        boolean res = pmsProductAttributeCategoryService.updateByIdFn(id, name);
        if(res){
            return  CommonResult.success(res);
        }else{
            return  CommonResult.failed();
        }
    }
}







































